<?php defined('SYSPATH') or die('No direct script access.');


class Controller_Admin_Finance_Income extends Stourweb_Controller
{
    private $channelname = "平台收入";
    public function before()
    {
        parent::before();
        $this->assign('prefix_url','finance/admin/finance/income');
    }

    //收入审核
    public function action_aduit()
    {
        $action = $this->params['action'];
        $modules = $this->_get_module();
        $pay_sources = Model_Member_Order::get_pay_source();
        $account_status = Model_Finance_Income::$account_status;
        $channelname = $this->channelname;
        $this->assign('channelname', $channelname);
        $this->assign('modules', $modules);
        $this->assign('pay_sources', $pay_sources);
        $this->assign('account_status', $account_status);
        if ($action=='excel')  //excel导出筛选
        {
            //生成导出excel页
            $this->assign('params',$_GET);
            $this->display('admin/finance/income/excel');
        }
        else
        {
            $channel = St_Functions::get_ota_channel(null,true);

            $this->assign('channel',$channel);
            $this->display('admin/finance/income/list');
        }
    }

    public function action_genexcel(){

        $timetype = Arr::get($_GET, 'timetype');//今日 昨日
        $time_target = Arr::get($_GET, 'time_target');//支付时间 下单时间
        $keyword = Arr::get($_GET, 'keyword');
        $status = $_GET['status'];
        $type_id = Arr::get($_GET, 'type_id');
        $channel_id = $_GET['channel_id'];
        $pay_method = $_GET['pay_method'];
        $starttime = empty($_GET['starttime'])? null:strtotime($_GET['starttime'].' 00:00:00');
        $endtime =empty($_GET['endtime'])?null: strtotime($_GET['endtime'].' 23:59:59');

        $time_arr = array();
        switch ($timetype)
        {
            case 1:
                $time_arr = St_Functions::back_get_time_range(1);
                break;
            case 2:
                $time_arr = St_Functions::back_get_time_range(2);
                break;
            case 3:
                $time_arr = St_Functions::back_get_time_range(3);
                break;
            case 5:
                $time_arr = St_Functions::back_get_time_range(5);
                break;
            case 6:
                $time_arr = array($starttime, $endtime);
                break;
        }


        $params = array(
            'type_id'  => $type_id,
            'pay_method' => $pay_method,
            'status'   => $status,
            'time_target' => $time_target,
            'start_time'=>$time_arr[0],
            'end_time'=>$time_arr[1],
            'channel_id' => $channel_id,
            'keyword'    => $keyword,
        );
        Model_Finance_Income::income_excel($params);


    }

    public function action_read()
    {
        $start = Arr::get($_GET, 'start');
        $limit = Arr::get($_GET, 'limit');
        $keyword = Arr::get($_GET, 'keyword');
        $status = $_GET['status'];
        $paysource = $_GET['paysource'];
        $type_id = $_GET['type_id'];
        $channel_id = $_GET['channel_id'];
        $pay_method = $_GET['pay_method'];
        $starttime = empty($_GET['startdate'])? null:strtotime($_GET['startdate'].' 00:00:00');
        $endtime =empty($_GET['startdate'])?null: strtotime($_GET['enddate'].' 23:59:59');

        $params = array(
            'type_id'    => $type_id,
            'channel_id' => $channel_id,
            'pay_method' => $pay_method,
            'paysource' => $paysource,
            'status'     => $status,
            'keyword'    => $keyword,
            'start'      => $start,
            'limit'      => $limit,
        );
        if($starttime && $endtime)
        {
            $params['time_target'] = 1;
            $params['start_time'] = $starttime;
            $params['end_time'] = $endtime;
        }
        $rows = Model_Finance_Income::get_search_list($params);
        $total = Model_Finance_Income::get_total_count($params);
        foreach($rows as &$item){
            if($item['pinyin']=='ship_line'){
                $item['pinyin']='ship';
            }
        }
        $result['total'] = $total;
        $result['lists'] = $rows;
        $result['success'] = true;

        exit(json_encode($result));
    }

    //详情页
    public function action_view()
    {

        $id = intval($this->params['id']);
        $info = Model_Finance_Income::get_info($id);
        $modules = $this->_get_module();
        $people_group = Model_Sysconfig::get_people_group(null,1);
        $this->assign('modules',$modules);
        $this->assign('people_group',$people_group);
        $this->assign('info', $info);
        $this->display('admin/finance/income/view');
    }

    //更新数据
    public function action_ajax_update()
    {
        $id = Arr::get($_POST, 'id');
        $filed = Arr::get($_POST, 'filed');
        $val = Arr::get($_POST, 'val');
        $ordersn = Arr::get($_POST, 'ordersn');
        $session = Session::instance();
        $operator = ORM::factory('admin', $session->get('userid'))
            ->get('username');
        if (! $operator)
        {
            exit(json_encode(array('status' => 0, 'msg' => '请重新登录后台')));
        }
        $arr = array(
            $filed     => $val,
            'ordersn'  => $ordersn,
            'operator' => $operator,
            'opt_time' => time(),
        );
        if (Model_Finance_Income::save_info($arr, $id))
        {
            exit(json_encode(array('status' => 1, 'msg' => '更新成功')));
        }

        exit(json_encode(array('status' => 0, 'msg' => '更新失败')));
    }

    //获取模型
    private  function _get_module()
    {
        $modules = Model_Finance_Income::get_all_pdt_modules();
        $user_id = Session::instance()->get('userid');

        $config_file = CACHE_DIR . 'newtravel/menu_' . $user_id . '.php';
        $memu=require $config_file;
        $temp=array();
        foreach($memu as $item)
        {
            if(!empty($item["url"]) && preg_match('~order/index~is',$item["url"]))
            {
                $temp[$item["typeid"]]=$item["id"];
            }
        }
        foreach($modules as &$item)
        {
            if(isset($temp[$item['id']]))
            {
                $item['menuid']=$temp[$item['id']];
            }
        }
        return $modules;
    }

    public function action_overview()
    {
        $menu_id = [
            1 => DB::select()->from('menu_new')->where('title', '=', '对账明细')->execute()->get('id'),
            2 => DB::select()->from('menu_new')->where('title', '=', '收入审核')->execute()->get('id'),
            3 => DB::select()->from('menu_new')->where('title', '=', '会员提现')->execute()->get('id'),
            4 => DB::select()->from('menu_new')->where('title', '=', '供应结算')->execute()->get('id'),
            5 => DB::select()->from('menu_new')->where('title', '=', '分销结算')->execute()->get('id'),
        ];

        $this->assign('menu_id', $menu_id);
        $this->display('admin/finance/income/overview');
    }

    public function action_ajax_get_statistics_data()
    {
        try {
            $day        = Arr::get($_POST, 'day');
            $start_date = Arr::get($_POST, 'start_date');
            $end_date   = Arr::get($_POST, 'end_date');
            if ($day == -1 && (!$start_date || !$end_date)) {
                throw new Exception('请选择时间');
            }
            if ($day > 0) {
                $start_date = strtotime(date('Y-m-d', strtotime('-' . $day . ' day')) . ' 23:59:59');
                $end_date   = strtotime(date('Y-m-d') . ' 23:59:59');
            } else {
                $start_date = strtotime($start_date . ' 00:00:00');
                $end_date   = strtotime($end_date . ' 23:59:59');
            }
            $data                   = [
                'start_date' => $start_date,
                'end_date'   => $end_date,
            ];
            $income_statistics      = Model_Finance_Overview::income_statistics($data);
            $expenditure_statistics = Model_Finance_Overview::expenditure_statistics($data);
            $supplier_statistics    = Model_Finance_Overview::supplier_statistics($data);
            $fenxiao_statistics=[];
            if(St_Functions::is_normal_app_install('mobiledistribution')){
                $fenxiao_statistics     = Model_Finance_Overview::fenxiao_statistics($data);
            }



            $res = [
                'status' => true,
                'data'   => [
                    'income_statistics'      => $income_statistics,
                    'expenditure_statistics' => $expenditure_statistics,
                    'supplier_statistics'    => $supplier_statistics,
                    'fenxiao_statistics'     => $fenxiao_statistics,
                ]
            ];
        } catch (Exception $e) {
            $res = [
                'status' => false,
                'msg'    => $e->getMessage(),
            ];
        }

        echo json_encode($res);
    }
}